package follow.juc.demo;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Restaurant {

    public String status = "cut";
    Lock lock = new ReentrantLock();
    Condition cut = lock.newCondition();
    Condition cook = lock.newCondition();
    Condition serving = lock.newCondition();

    public void cut() {
        lock.lock();
        try {
            while (!status.equals("cut")) {
                try {
                    cut.await();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            System.out.println(Thread.currentThread().getName() + "开始切菜");

            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            status = "cook";
            cook.signal();
        } finally {
            lock.unlock();
        }

    }

    public void cook() {
        lock.lock();
        try {
            while (!status.equals("cook")) {
                try {
                    cook.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println(Thread.currentThread().getName() + "开始炒菜");
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            status = "serving";
            serving.signal();
        } finally {

        }

    }

    public void serving() {
        lock.lock();
        try {
            while (!status.equals("serving")) {
                try {
                    serving.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println(Thread.currentThread().getName() + "开始上菜");
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            status = "cut";
            cut.signal();
        } finally {
            lock.unlock();
        }

    }


}
